package com.wen.update;

import android.app.Activity;
import android.content.Intent;

import com.wen.update.listener.DefActivityLifecycleCallbacks;
import com.wen.update.listener.OnUpdateListener;

/**
 * 更新App的Utils类
 */

public class UpdateUtils {
    /**
     * 更新APK
     */
    public static void update(final Activity context, String apkUrl, String versionName, OnUpdateListener updateListener) {
        // TODO 添加权限的问题
        UpdateService.mUpdateListener = updateListener;
        if (updateListener != null) {
            context.getApplication().registerActivityLifecycleCallbacks(new DefActivityLifecycleCallbacks() {
                @Override
                public void onActivityDestroyed(Activity activity) {
                    UpdateService.mUpdateListener = null;
                    context.getApplication().unregisterActivityLifecycleCallbacks(this);
                }
            });
        }
        if (UpdateService.isLoading) {
            return;
        }
        Intent intent = new Intent(context, UpdateService.class);
        intent.setAction(UpdateService.SERVICE_ACTION);
        intent.putExtra(UpdateService.APK_URL, apkUrl);
        intent.putExtra(UpdateService.VERSION_NAME, versionName);
        context.startService(intent);

    }

    /**
     * 更新APK
     */
    public static void update(Activity context, String apkUrl, String versionName) {
        update(context, apkUrl, versionName, null);
    }
}
